package com.fp.m7.model;

import java.io.Serializable;
import java.util.Date;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "comments")
public class Comment implements com.fp.m7.api.model.Comment, HasDate,
		Comparable<Comment> {
	private static final long serialVersionUID = -1643453134582786089L;

	@Id
	private ObjectId id;

	@Transient
	private Account author;
	@Indexed
	private ObjectId authorId;

	private String content;
	@Indexed
	private Long date;

	@PersistenceConstructor
	public Comment(ObjectId id, ObjectId authorId, String content, Long date) {
		this.id = id;
		this.authorId = authorId;
		this.content = content;
		this.date = date;
	}

	public Comment(Account author, String content) {
		this.author = author;
		if (author != null) {
			this.authorId = author.getId();
		}
		this.content = content;
	}

	@Override
	public ObjectId getId() {
		return id;
	}

	@Override
	public void setId(Serializable id) {
		this.id = (ObjectId) id;
	}

	@Override
	public Account getAuthor() {
		return author;
	}

	@Override
	public void setAuthor(com.fp.m7.api.model.Account author) {
		this.author = (Account) author;
	}

	public ObjectId getAuthorId() {
		return authorId;
	}

	public void setAuthorId(ObjectId authorId) {
		this.authorId = authorId;
	}

	@Override
	public String getContent() {
		return content;
	}

	@Override
	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public Date getIssuedDate() {
		return new Date(date);
	}

	@Override
	public void setIssuedDate(Date date) {
		this.date = date == null ? null : date.getTime();
	}

	@Override
	public Long getDate() {
		return date;
	}

	@Override
	public void setDate(Long date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return new StringBuilder("Comment [content=").append(content) //
				.append(", auditCD=").append(date) //
				.append("]").toString();
	}

	@Override
	public int compareTo(Comment that) {
		return that.getDate().compareTo(this.getDate());
	}

}
